const fs = require('fs');
const fileService = require('../service/file.service');
const {WALL_PATH} = require('../constants/file-path');

class WallController {

  // 返回用户壁纸
  async wallInfo(ctx, next) {
    try {
      // 1.获取参数
      const {filename} = ctx.params;
      // 2.查询数据
      const fileInfo = await fileService.getWallByFilename(filename);
      // 3.返回数据
      ctx.response.set('content-type', fileInfo.mimetype);
      ctx.body = fs.createReadStream(`${WALL_PATH}/${filename}`);
    } catch (error) {
      console.log(error);
    }
  }

  // 获取所有用户壁纸
  async list(ctx, next) {
    // 1.获取参数
    const {userId} = ctx.params;
    // 2.操作数据库
    const result = await fileService.getWallList(userId);
    // 3.返回结果
    ctx.body = result;
  }

  // 删除某张壁纸
  async remove(ctx, next) {
    // 1.获取参数
    const {wallId} = ctx.request.body;
    const {id} = ctx.user;
    // 2.删除操作
    const result = await fileService.removeWall(wallId, id);
    // 3.返回结果
    ctx.body = result;
  }

}

module.exports = new WallController();
